﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class city_s_forum_show : System.Web.UI.Page
{

    int no;

    protected void Page_Load(object sender, EventArgs e)
    {

        no = int.Parse(Request["no"]);

        if (!this.IsPostBack)
        {
            //init helper class
            GeoLingManager glManager = new GeoLingManager(Request, Response);
            String _lang = glManager.getLang();
            DatabaseUtil dbUtil = new DatabaseUtil();
            //Get user's preferred langugage in int
            _lang = dbUtil.getLanguageNo(_lang).ToString();
            ArrayList al = new ArrayList();
            ctpDataContext db = new ctpDataContext();

            ct_s_forum fou = db.ct_s_forums.Single(p => p.no == no);
            ct_s_profile cts = db.ct_s_profiles.Single(p => p.valid == true && p.no ==fou.ct_s_no && p.language_no == (int)Session["now_user_lang_no"]);

            fou.view_amount += 1;
            db.SubmitChanges();
            
            this.HyperLink3.Text = cts.title;
            this.HyperLink3.NavigateUrl = "city_s.aspx?no=" + cts.no.ToString();
            this.HyperLink2.Text = cts.ct_profile.title.Trim();
            this.HyperLink2.NavigateUrl = "city.aspx?no=" + cts.ct_no;

            this.HyperLink9.NavigateUrl = "city_s_forum.aspx?no=" + cts.no.ToString();

           // this.ViewState["ct_no"] = cts.ct_no;

            var data = from p in db.ct_s_forums
                       where p.reply_to == no || p.no == no
                       orderby p.addtime
                       select p;
            var first = data.First();
            String title = dbUtil.getPropertyPost(first.title, (int)DatabaseUtil.fromTables.forum_title, first.no,_lang);
            this.Label4.Text = title;
        
            foreach (ct_s_forum f in data)
            {
                if (f.user.profile_picture != null)
                    f.user.profile_picture = f.user.profile_picture.Trim();
                else
                    f.user.profile_picture = "";

                f.text = f.text.Replace("\n","<br>");
                al.Add(f.no);
            }
            Hashtable titlesTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.forum_title, DataUtil.ObjsToInts(al.ToArray()));
            Hashtable textsTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.forum_text, DataUtil.ObjsToInts(al.ToArray()));
            foreach (ct_s_forum f in data)
            {
                f.text = dbUtil.getPropertyPost(f.text, textsTable, f.no, _lang);
                f.title = dbUtil.getPropertyPost(f.title, titlesTable, f.no, _lang);
            }
            this.DataList1.DataSource = data;
            this.DataBind();

            this.HyperLink11.NavigateUrl = "city_s_forum_reply.aspx?no="+cts.no+"&reply="+no.ToString();

        }
    }
}
